<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%>
<%@page import="org.springframework.web.context.WebApplicationContext"%>
<%@page import="org.springframework.web.context.support.WebApplicationContextUtils"%>
<%@page import="db.Project"%>
<%@page import="java.text.SimpleDateFormat"%>
<%@page import="db.ProjectDAO"%>
<%@page import="db.OutpurmatDAO"%>
<%@page import="db.TobDAO" %>
<%@page import="db.Tob" %>
<%@page import="db.TeamDAO" %>
<%@page import="db.Team" %>
<%@page import="db.PurreqDAO" %>
<%@page import="db.Retreq" %>
<%@page import="db.RetreqDAO" %>
<%@page import="db.SupplierDAO" %>
<%@page import="db.User"%>
<%@page import="db.UserDAO"%>
<%@page import="db.Group"%>
<%@page import="db.Purmat"%>
<%@page import="db.PurmatDAO"%>
<%@page import="db.Material"%>
<%@page import="db.MaterialDAO"%>
<%@page import="db.Outpurmat"%>
<%@page import="db.Outreq"%>
<%@page import="db.OutreqDAO"%>
<%@page import="db.Retmat"%>
<%@page import="db.RetmatDAO"%>
<%@page import="db.Receivepurmat"%>
<%@page import="db.ReceivepurmatDAO"%>
<%@ include file="/jsp/common/utility/volti.jsp" %>
<%@ include file="/jsp/common/utility/logined.jsp" %>
<%
	request.setCharacterEncoding("utf-8");
	boolean success = false;
	WebApplicationContext ctx = WebApplicationContextUtils.getWebApplicationContext(getServletContext());
	PurreqDAO purreqDAO = (PurreqDAO)ctx.getBean("PurreqDAO");
	OutreqDAO outreqDAO = (OutreqDAO)ctx.getBean("OutreqDAO");
	TobDAO tobDAO = (TobDAO)ctx.getBean("TobDAO");
	ReceivepurmatDAO receivepurmatDAO = (ReceivepurmatDAO)ctx.getBean("ReceivepurmatDAO");
	TeamDAO teamDAO = (TeamDAO)ctx.getBean("TeamDAO");
	SupplierDAO supplierDAO = (SupplierDAO)ctx.getBean("SupplierDAO");
	UserDAO userDAO = (UserDAO)ctx.getBean("UserDAO");
	RetreqDAO retreqDAO = (RetreqDAO)ctx.getBean("RetreqDAO");
	MaterialDAO materialDAO = (MaterialDAO)ctx.getBean("MaterialDAO");
	OutpurmatDAO outpurmatDAO = (OutpurmatDAO)ctx.getBean("OutpurmatDAO");
	RetmatDAO retmatDAO = (RetmatDAO)ctx.getBean("RetmatDAO");
	String pid = request.getParameter("pid");
	String[] matidtype = request.getParameterValues("matidtype");
	String[] matidvalue = request.getParameterValues("matidvalue");
	String[] matnum = request.getParameterValues("shenpi");
	int userid = (Integer)session.getAttribute("id");
	List list =new ArrayList();
	String message = "";
	
	
	
	
	
	if(matidtype.length>0 && pid!=null){//确认有勾选物资
		for(int i=0;i<matidtype.length;i++){
			if(matidtype[i]=="1" || "1".equals(matidtype[i])){//有效
				Retmat retmat = retmatDAO.findById(Integer.parseInt(matidvalue[i]));
				Outpurmat outpurmat = retmat.getOutpurmat();
				//最终退库
				retmat.setState(3);//正式出库.setOkNum(Integer.parseInt(matnum[i]));
				//减去正式退库数
				Outpurmat outpurmatok = retmat.getOutpurmat();
				int duoyu = retmat.getRetNum();//原始申请数量
				int zhengshi = Integer.parseInt(matnum[i]);//最终出库数量
				int addnum = duoyu - zhengshi;
				int un = outpurmatok.getRetUseNum();//出库可用数量
				int retn = outpurmatok.getRetNum();
				outpurmatok.setRetUseNum(un+addnum);
				outpurmatok.setRetNum(retn-zhengshi);//正是退库数量
				
				//为总库存表增加可用数量和库存数量
				Receivepurmat receivepurmat = outpurmatok.getReceivepurmat();
				int reimportnum = receivepurmat.getImportNum();
				int usnum = receivepurmat.getUseNum();
				receivepurmat.setUseNum(usnum+zhengshi);
				receivepurmat.setImportNum(reimportnum+zhengshi);
				receivepurmatDAO.attachDirty(receivepurmat);
				outpurmatDAO.attachDirty(outpurmatok);
				retmatDAO.attachDirty(retmat);
			}else{
				//没有勾选归还可退数
				Retmat retmat = retmatDAO.findById(Integer.parseInt(matidvalue[i]));
				Outpurmat outpurmat = retmat.getOutpurmat();
				
				int duoyu = retmat.getRetNum();
				int un = outpurmat.getRetUseNum();
				outpurmat.setRetUseNum(un+duoyu);
				retmat.setState(2);//无效状态
				outpurmatDAO.attachDirty(outpurmat);
				retmatDAO.attachDirty(retmat);
				
			}
		}
		
		//更新审批状态
		Retreq retreq = retreqDAO.findById(Integer.parseInt(pid));
		retreq.setPhase(5);//流程结束
		retreq.setOkDate(new Date());
		retreq.setOkState(2);//审批通过
		retreq.setOkUserId(userid);
		retreq.setOkState(2);//等待审批
		retreqDAO.attachDirty(retreq);
		success = true;
		message = "恭喜您！出库确认成功！";
	}else{
		message = "出库确认失败！";
		success = false;
	}

	request.setAttribute("success",success);
	request.setAttribute("message",message);
	pageContext.forward("/vm/returnStock/retAppmessage1.vm");
%>
<%!
	public List getListReMap(List list){
		List listmap = new ArrayList();
		//System.out.println("===============>>>>>>>>");
		if(list != null && list.size()>0){
		
			for(int i=0;i<list.size();i++){
			
				Map map = new HashMap();
				Object[] aa= (Object[]) list.get(i);
				map.put("pid", aa[0]);
				map.put("pstate", aa[1]);
				map.put("pcode", aa[2]);
				map.put("procode", aa[3]);
				map.put("proname", aa[4]);
				SimpleDateFormat sf=new SimpleDateFormat("yyyy-MM-dd"); 
				if(aa[5]!=null){
					map.put("pdate", sf.format(aa[5]));
				}
				
				if(aa[6]!=null){
					map.put("penddate", sf.format(aa[6]));
				}
				
				listmap.add(map);
			}
		}
		return listmap;
	}
	
	public List getListReMap2(List list,MaterialDAO materialDAO){
		List listmap = new ArrayList();
		if(list != null && list.size()>0){
			for(int i=0;i<list.size();i++){
				Map map = new HashMap();
				Purmat purmat = new Purmat();
				purmat = (Purmat)list.get(i);
				if(purmat!=null){
					map.put("purid", purmat.getId());//
					map.put("purnum", purmat.getNumber());//申购数量
					map.put("purprice",purmat.getPrice());//申购价格
					//根据物质id关联物质编码和名称
					if(purmat.getMaterialId()!=null){
						int mid = purmat.getMaterialId();
						Material material = new Material();
						material = materialDAO.findById(mid);
						map.put("matcode", material.getCode());
						map.put("matname", material.getName());
						map.put("matcom", material.getCompany());
					}
				}
				listmap.add(map);
			}
		}
		return listmap;
	}
%>